package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.SupplyDTO;
import net.wanho.po.Supply;
import net.wanho.service.SupplyService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/supply")
public class SupplyController {

    private final SupplyService supplyService;

    @GetMapping("/supply-list")
    public AjaxResult supplyList() {
        return AjaxResult.success(supplyService.list());
    }

    @GetMapping("/supply-page-list")
    public AjaxResult supplyPageList(@RequestParam(defaultValue = "1") int pageNum,
                                     @RequestParam(defaultValue = "5") int pageSize,
                                     SupplyDTO supplyDTO) {
        PageInfo<Supply> pageInfo = supplyService.pageList(pageNum, pageSize, supplyDTO);
        return AjaxResult.success("success", pageInfo);
    }

    @GetMapping("/supply-num-check")
    public AjaxResult supplyNumCheck(@RequestParam String supplyNum) {
        Boolean supplyNumCheck = supplyService.selectIdBySupplyName(supplyNum);
        return AjaxResult.success("success", supplyNumCheck);
    }

    @PostMapping("/supply-add")
    public AjaxResult supplyAdd(@RequestBody Supply supply) {
        supply.setIsDelete("0");
        supplyService.save(supply);
        return AjaxResult.success("供应商添加成功");
    }

    @PutMapping("/supply-update")
    public AjaxResult supplyUpdate(@RequestBody Supply supply) {
        supplyService.updateById(supply);
        return AjaxResult.success("供应商修改成功");
    }

    @DeleteMapping("/supply-delete/{id}")
    public AjaxResult supplyDelete(@PathVariable Integer id) {
        supplyService.removeById(id);
        return AjaxResult.success("供应商删除成功");
    }

    @GetMapping("/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  SupplyDTO supplyDTO) {
        PageInfo<Supply> pageInfo = supplyService.pageList(pageNum, pageSize, supplyDTO);
        return AjaxResult.success("success", pageInfo.getResultList());
    }
}
